﻿using Common.Extensions.OrderBy;
using DomainModel;
using IDataAccess;
using IManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Manager
{
    public class ManagerContent : IManagerContent
    {
        /// <summary>
        /// 内容信息数据访问接口
        /// </summary>
        public IDataAccessContent IDAContent { get; set; }

        public bool AddContent(ArticleInfo article)
        {
           var res = IDAContent.Create(article);
           IDAContent.UnitOfWork.Commit();
           if (res!=null)
           {
               return true;
           }
           else
           {
               return false;
           }
        }

        public bool DeleteContent(IList<int> articleIdList)
        {
            try
            {
                foreach (var item in articleIdList)
                {
                    IDAContent.Delete(item);
                }
               
                IDAContent.UnitOfWork.Commit();
                return true;
            }
            catch (Exception)
            {
                return false;
            }    
        }

        public bool UpdateContent(ArticleInfo article)
        {
            try
            {
                IDAContent.Update(article);
                IDAContent.UnitOfWork.Commit();
                return true;
            }
            catch (Exception)
            {
                return false;
            }    
        }

        public IList<ArticleInfo> LoadAll(Expression<Func<ArticleInfo, bool>> where, int pageIndex, int pageSize, params IOrderByExpression<ArticleInfo>[] orderByExpressions)
        {
            return IDAContent.Filter(where, pageIndex, pageSize, orderByExpressions).ToList();
        }

        public int LoadCount(Expression<Func<ArticleInfo, bool>> where)
        {
            return IDAContent.Count(where);
        }

        public ArticleInfo GetArticle(int articleId)
        {
            IList<string> list = new List<string>();
            list.Add("ArticleDetail");

            Expression<Func<ArticleInfo, bool>> where = e => e.ContentId == articleId;
            return IDAContent.Find(where, list);
        }
    }
}
